const express = require("express");
const fs = require("fs");
const path = require("path");
const app = express();
const PORT = process.env.PORT || 3000;

// 中间件设置
app.use(express.json());

// CORS设置
app.use((req, res, next) => {
    res.header("Access-Control-Allow-Origin", "*");
    res.header(
        "Access-Control-Allow-Headers",
        "Origin, X-Requested-With, Content-Type, Accept"
    );
    next();
});

// 获取普通模型列表
app.get("/api/models/normal", (req, res) => {
    try {
        const filePath = path.join(__dirname, "normal.model.json");
        const data = fs.readFileSync(filePath, "utf8");
        const models = JSON.parse(data);

        res.json({
            code: 200,
            message: "获取普通模型列表成功",
            data: models,
        });
    } catch (error) {
        console.error("获取普通模型列表失败:", error);
        res.status(500).json({
            code: 500,
            message: "获取普通模型列表失败",
            error: error.message,
        });
    }
});

// 获取推理模型列表
app.get("/api/models/reason", (req, res) => {
    try {
        const filePath = path.join(__dirname, "reason.model.json");
        const data = fs.readFileSync(filePath, "utf8");
        const models = JSON.parse(data);

        res.json({
            code: 200,
            message: "获取推理模型列表成功",
            data: models,
        });
    } catch (error) {
        console.error("获取推理模型列表失败:", error);
        res.status(500).json({
            code: 500,
            message: "获取推理模型列表失败",
            error: error.message,
        });
    }
});

// 404处理
app.use((req, res) => {
    res.status(404).json({
        code: 404,
        message: "请求的资源不存在",
    });
});

// 启动服务器
app.listen(PORT, () => {
    console.log(`服务器已启动，监听端口: ${PORT}`);
});
